﻿/*
editor_value.js

The MIT License

Copyright (c) 2011 Yuya.Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// グローバル変数エディタ

//! グローバル変数エディタクラス
function EspEditorValue() {
	this.isSelectGroup = true;
	this.selectGroup = 0;
	this.selectInstance = 0;
}
//! EspEditorBaseを継承する
EspEditorValue.prototype = new EspEditorBase(espMessage.window.editorValue, 520, 300);
EspEditorValue.prototype.constructor = EspEditorValue;

//! 定数値
EspEditorValue.tagNameSelAll = "selAll";
EspEditorValue.tagNameNameNew = "nameNew";
EspEditorValue.tagNameValNew = "valNew";
EspEditorValue.tagNameDefNew = "defNew";

EspEditorValue.tagNameSel = "sel";
EspEditorValue.tagNameName = "name";
EspEditorValue.tagNameVal = "val";
EspEditorValue.tagNameDef = "def";

//! 新規追加グループ名
EspEditorValue.tagGroupNameGroupNew = "g_gnameNew";
EspEditorValue.tagGroupNameInstanceNew = "g_inameNew";
//! 新規変数定義名
EspEditorValue.tagGroupNameNameNew = "g_nameNew";
//! 新規追加定義値
EspEditorValue.tagGroupNameDefNew = "g_defNew";

EspEditorValue.tagGroupNameSel = "g_sel";
//! グループ名変更
EspEditorValue.tagGroupNameGroupName = "g_gname";
EspEditorValue.tagGroupNameInstanceName = "g_iname";
EspEditorValue.tagGroupNameName = "g_name";
EspEditorValue.tagGroupNameVal = "g_val";
EspEditorValue.tagGroupNameDef = "g_def";

//! 基本パラメータの再描画
EspEditorValue.prototype.redrawNormalValue = function(){
	var valCount = espGlobalValue.getCount();
	var text = "";
	text += "<table>";
	text += "<tr><td>("+valCount+")</td><td>"+espMessage.editorValue.name+"</td><td>"+espMessage.editorValue.defVal+"</td><td>"+espMessage.editorValue.dbgVal+"</td><td>"+espMessage.editorValue.edit+"</td></tr>";
	for(var i = 0; i < valCount; i++){
		var val = espGlobalValue.getValue(i);
		text += "<tr><td>"+(i+1)+"</td><td><input type='checkbox' name='"+EspEditorValue.tagNameSel+"' onchange='window.subcomponent.modifiData("+i+");'><input type='text' value='"+val.name+"' size='12' name='"+EspEditorValue.tagNameName+"' onchange='window.subcomponent.modifiData("+i+");'></td><td><input type='text' value='"+val.def+"' size='8' name='"+EspEditorValue.tagNameDef+"' onchange='window.subcomponent.modifiData("+i+");'></td><td><input type='text' value='"+val.val+"' size='8' name='"+EspEditorValue.tagNameVal+"' onchange='window.subcomponent.modifiData("+i+");'></td><td><button onclick='window.subcomponent.resetData("+i+");'>"+espMessage.editorValue.reset+"</button><button onclick='window.subcomponent.removeData("+i+");'>"+espMessage.editorValue.remove+"</button></td></tr>";
	}
	var textPreTag = "";
	var textPostTag = "";
	if(valCount > 0){
		textPreTag = "<input type='checkbox' name='"+EspEditorValue.tagNameSelAll+"' onclick='window.subcomponent.updateCheckBox();'>";
		textPostTag = "<button onclick='window.subcomponent.resetDataAll();'>"+espMessage.editorValue.resetAll+"</button><button onclick='window.subcomponent.removeSelect();'>"+espMessage.editorValue.removeSelect+"</button>";
	}
	text += "<tr><td>N/A</td><td>"+textPreTag+"<input type='text' value='val' size='12' name='"+EspEditorValue.tagNameNameNew+"'></td><td><input type='text' value='0' size='8' name='"+EspEditorValue.tagNameDefNew+"'></td><td><input type='text' value='0' size='8' name='"+EspEditorValue.tagNameValNew+"'></td><td><button onclick='window.subcomponent.addData();'>"+espMessage.editorValue.add+"</button>"+textPostTag+"</td></tr>";
	text += "</table>";
	return text;
}

EspEditorValue.prototype.redrawGroupValue = function(){
	var text = "";
	text += "<table>";
	text += "<tr><td>"+espMessage.editorValue.group+"</td><td>"+espMessage.editorValue.propaty+"</td></tr>";
	text += "<tr><td>";
	// グループとインスタンスをツリー表示
	text += "<ul><div>-group tree</div>";
	var groupCount = espGlobalValue.getGroupCount();
	for(var i = 0; i < groupCount; i++){
		var gname = espGlobalValue.getGroupName(i);
		var instCount = espGlobalValue.getInstanceCount(i);
		if((this.isSelectGroup == true) && (this.selectGroup == i)){
			// 選択中のグループは名前を変更できるようにする
			text += "<ul>-<span class='selected'><input type='text' value='"+gname+"' size='12' name='"+EspEditorValue.tagGroupNameGroupName+"' onchange='window.subcomponent.setGroupName("+i+")'>";
		}else{
			text += "<ul>-<span style='cursor:pointer;' onclick='window.subcomponent.setSelectGroup("+i+");'>"+gname;
		}
		text += " ("+instCount+")<button onclick='window.subcomponent.removeGroup("+i+");'>"+espMessage.editorValue.remove+"</button></span>";

		for(var j = 0; j< instCount; j++){
			var iname = espGlobalValue.getInstanceName(i,j);
			if((this.isSelectGroup != true) && (this.selectGroup == i) && (this.selectInstance == j)){
				text += "<ul><span class='selected'>";
				text += "<input type='text' value='"+iname+"' size='12' name='"+EspEditorValue.tagGroupNameInstanceName+"' onchange='window.subcomponent.setInstanceName("+i+","+j+")'>";
			}else{
				text += "<ul><span style='cursor:pointer;' onclick='window.subcomponent.setSelectInstance("+i+","+j+");'>";
				text += iname;
			}
			text += "</span><button onclick='window.subcomponent.removeInstance("+i+","+j+");'>"+espMessage.editorValue.remove+"</button></ul>";
		}
		// インスタンス追加インターフェース
		text += "<ul>&lt;&lt;<input type='text' value='"+"instance"+"' size='12' name='"+EspEditorValue.tagGroupNameInstanceNew+"'><button onclick='window.subcomponent.addInstance("+i+");'>"+espMessage.editorValue.add+"</button></ul>";
		text += "</ul>";
	}
	// 最後にグループ追加用のフィールドを設ける
	text += "<ul>&lt;&lt;<input type='text' value='"+"group"+"' size='12' name='"+EspEditorValue.tagGroupNameGroupNew+"'><button onclick='window.subcomponent.addGroup();'>"+espMessage.editorValue.add+"</button></ul>";
	text += "</ul>";
	text += "</td><td>";
	// パラメータ編集領域
	if(this.isSelectGroup == true){
		if(groupCount > this.selectGroup){
			// グループのパラメータを表示
			text += "<table>";
			text += "<tr><td>"+espMessage.editorValue.name+"</td><td>"+espMessage.editorValue.defVal+"</td><td>"+espMessage.editorValue.edit+"</td></tr>";
			var defCount = espGlobalValue.getGroupDefCount(this.selectGroup);
			for(var j = 0; j < defCount; j++){
				var def = espGlobalValue.getGroupDef(this.selectGroup, j);
				text += "<tr><td><input type='text' value='"+def.name+"' size='12' name='"+EspEditorValue.tagGroupNameName+"' onchange='window.subcomponent.modifiGroupDef("+this.selectGroup+","+j+");'></td><td><input type='text' value='"+def.def+"' size='12' name='"+EspEditorValue.tagGroupNameDef+"' onchange='window.subcomponent.modifiGroupDef("+this.selectGroup+","+j+");'></td><td><button onclick='window.subcomponent.removeGroupDef("+this.selectGroup+","+j+");'>"+espMessage.editorValue.remove+"</button></td></tr>";
			}
			// 最後にパラメータ追加用のフィールドを追加
			text += "<tr><td><input type='text' value='"+"value"+"' size='12' name='"+EspEditorValue.tagGroupNameNameNew+"'></td><td><input type='text' value='"+0+"' size='12' name='"+EspEditorValue.tagGroupNameDefNew+"'></td><td><button onclick='window.subcomponent.addGroupDef("+this.selectGroup+");'>"+espMessage.editorValue.add+"</button></tr></td>";
			text += "</table>";
		}
	}else{
		if(groupCount > this.selectGroup){
			var gname = espGlobalValue.getGroupName(this.selectGroup);
			var instCount = espGlobalValue.getInstanceCount(this.selectGroup);
			if(instCount > this.selectInstance){
				// インスタンスのパラメータを表示
				text += "<table>";
				text += "<tr><td>"+espMessage.editorValue.name+"</td><td>"+espMessage.editorValue.dbgVal+"</td><td>"+espMessage.editorValue.edit+"</td></tr>";
				var defCount = espGlobalValue.getGroupDefCount(this.selectGroup);
				var inst = espGlobalValue.getInstance(this.selectGroup, this.selectInstance);
				for(var j = 0; j < defCount; j++){
					var def = espGlobalValue.getGroupDef(this.selectGroup, j);
					text += "<tr><td>"+def.name+"</td><td><input type='text' value='"+inst.get(j)+"' size='12' name='"+EspEditorValue.tagGroupNameVal+"' onchange='window.subcomponent.modifiInstanceValue("+this.selectGroup+", "+this.selectInstance+", "+j+");'></td><td><button>"+espMessage.editorValue.reset+"</button></td></tr>";
				}
				text += "</table>";
			}
		}
	}

	text += "</td></tr></table>";
	return text;
}

//! 再描画関数をオーバーライドする
EspEditorValue.prototype.redraw = function(){
	var text = this.redrawNormalValue() + this.redrawGroupValue();
	this.writedoc(text);
}

//! 新しくデータを追加する
EspEditorValue.prototype.addData = function(){
	// 追加用フィールドを読み込む
	var names = this.window.document.getElementsByName(EspEditorValue.tagNameNameNew);
	var vals = this.window.document.getElementsByName(EspEditorValue.tagNameValNew);
	var defs = this.window.document.getElementsByName(EspEditorValue.tagNameDefNew);
	espGlobalValue.addNewValue(names[0].value, defs[0].value, vals[0].value);
	// 再描画
	this.redraw();
}

//! 新しくグループを追加する
EspEditorValue.prototype.addGroup = function(){
	// 追加用フィールドを読み込む
	var names = this.window.document.getElementsByName(EspEditorValue.tagGroupNameGroupNew);
	espGlobalValue.addGroup(names[0].value);
	this.redraw();
}

//! グループを削除する
EspEditorValue.prototype.removeGroup = function(group){
	espGlobalValue.removeGroup(group);
	this.redraw();
}

//! 新しくインスタンスを追加する
EspEditorValue.prototype.addInstance = function(group){
	// 追加用フィールドを読み込む
	var names = this.window.document.getElementsByName(EspEditorValue.tagGroupNameInstanceNew);
	espGlobalValue.addInstance(group, names[group].value);
	this.redraw();
}

//! インスタンスを削除する
EspEditorValue.prototype.removeInstance = function(group, index){
	espGlobalValue.removeInstance(group, index);
	this.redraw();
}

//! グループを選択した
EspEditorValue.prototype.setSelectGroup = function(index){
	this.isSelectGroup = true;
	this.selectGroup = index;
	this.redraw();
}
//! インスタンスを選択した
EspEditorValue.prototype.setSelectInstance = function(group,instance){
	this.isSelectGroup = false;
	this.selectGroup = group;
	this.selectInstance = instance;
	this.redraw();
}

//! グループ名を変更した
EspEditorValue.prototype.setGroupName = function(group){
	// 名前を取得する
	var names = this.window.document.getElementsByName(EspEditorValue.tagGroupNameGroupName);
	espGlobalValue.setGroupName(group, names[0].value);
	// 再描画は不要なので行わない
}

//! インスタンス名を変更した
EspEditorValue.prototype.setInstanceName = function(group, inst){
	// 名前を取得する
	var names = this.window.document.getElementsByName(EspEditorValue.tagGroupNameInstanceName);
	espGlobalValue.setInstanceName(group, inst, names[0].value);
	// 再描画は不要なので行わない
}

//! グループ定義を追加した
EspEditorValue.prototype.addGroupDef = function(group){
	// 追加インターフェースを読み取る
	var names = this.window.document.getElementsByName(EspEditorValue.tagGroupNameNameNew);
	var defs = this.window.document.getElementsByName(EspEditorValue.tagGroupNameDefNew);
	espGlobalValue.addGroupDef(group, names[0].value, defs[0].value);
	this.redraw();
}

//! グループ定義を削除する
EspEditorValue.prototype.removeGroupDef = function(group, index){
	espGlobalValue.removeGroupDef(group, index);
	this.redraw();
}

//! グループ定義を変更した
EspEditorValue.prototype.modifiGroupDef = function(group, index){
	var names = this.window.document.getElementsByName(EspEditorValue.tagGroupNameName);
	var defs = this.window.document.getElementsByName(EspEditorValue.tagGroupNameDef);
	espGlobalValue.setGroupDef(group, index, names[index].value, defs[index].value);
	// 再描画は不要なので行わない
}

//! グループインスタンスの値を変更した
EspEditorValue.prototype.modifiInstanceValue = function(group, instance, index){
	var vals = this.window.document.getElementsByName(EspEditorValue.tagGroupNameVal);
	espGlobalValue.setInstanceValue(group, instance, index, vals[index].value);
	// 再描画は不要なので行わない
}

//! データを削除する
EspEditorValue.prototype.removeData = function(index){
	espGlobalValue.removeValue(index);
	// 再描画
	this.redraw();
}

//! 選択データを削除する
EspEditorValue.prototype.removeSelect = function(){
	var sels = this.window.document.getElementsByName(EspEditorValue.tagNameSel);
	var selCount = sels.length;
	for(var i = selCount - 1; i >= 0; i--){
		if(sels[i].checked){
			espGlobalValue.removeValue(i);
		}
	}
	// 再描画
	this.redraw();
}

//! データを変更する
EspEditorValue.prototype.modifiData = function(index){
	var sels = this.window.document.getElementsByName(EspEditorValue.tagNameSel);
	var names = this.window.document.getElementsByName(EspEditorValue.tagNameName);
	var vals = this.window.document.getElementsByName(EspEditorValue.tagNameVal);
	var defs = this.window.document.getElementsByName(EspEditorValue.tagNameDef);	
	espGlobalValue.modifiValue(index, names[index].value, defs[index].value, vals[index].value);
	// 再描画は不要
}


//! チェックボックスの状態を一括変更する
EspEditorValue.prototype.updateCheckBox = function(){
	var sel = this.window.document.getElementsByName(EspEditorValue.tagNameSelAll);
	var sels = this.window.document.getElementsByName(EspEditorValue.tagNameSel);
	var selCount = sels.length;
	for(var i = 0; i < selCount; i++){
		sels[i].checked = sel[0].checked;
	}
}

//! データを初期化する
EspEditorValue.prototype.resetData = function(index){
	var defs = this.window.document.getElementsByName(EspEditorValue.tagNameDef);
	espGlobalValue.set(index, defs[index].value);
	// 再描画
	this.redraw();
}

//! データを初期化する
EspEditorValue.prototype.resetDataAll = function(){
	espGlobalValue.reset();
	// 再描画
	this.redraw();
}

var espEditorValue = new EspEditorValue();
